///////////////////////////////////////////////////////////////////////////////
//
//
//
///////////////////////////////////////////////////////////////////////////////

package model {

    import flash.events.Event;
    import flash.net.URLRequestMethod;

    import model.service.IElfinService;

    import model.vo.ConfigVO;
    import model.vo.CountryVO;

    import org.kolonitsky.alexey.net.ElfinSerivce;


    /**
     *  Store and manipulate static country static dictionary.
     */
    public class DictionaryProxy extends ElfinSafeProxy
    {
        public static const NAME:String = "dictionaryProxy";

        public static const UPDATE:String = "dictionaryProxyUpdate";

        public function get dictionary():Vector.<CountryVO>
        {
            return data as Vector.<CountryVO>;
        }


        //---------------------------------------------------------------------
        //  Methods
        //---------------------------------------------------------------------

        /**
         * Constructor
         * @param config
         */
        public function DictionaryProxy(config:ConfigVO)
        {
            super(NAME, new Vector.<CountryVO>, config);
        }

        /**
         *  Retrieve all static information about countries.
         */
        public function getCountyInfo():void
        {
            if (dictionary && dictionary.length)
            {
                sendNotification(UPDATE, dictionary);
                return;
            }

            var service:IElfinService = createElfinService(ElfinSerivce, service_completeHandler);
            service.load(Config.COUNTY_INFO_GET, URLRequestMethod.POST);
        }



        //-------------------------------------------------------------------------
        //
        //  Private logic
        //
        //-------------------------------------------------------------------------

        private function service_completeHandler(event:Event):void
        {
            var service:ElfinSerivce = event.currentTarget as ElfinSerivce;
            var array:Array = service.lastData as Array;
            var result:Vector.<CountryVO> = new Vector.<CountryVO>();

            for each(var country:Object in array)
                if ("currencyCode" in country && country["currencyCode"])
                    result.push(new CountryVO(country));
                else
                    log("Currency info expected. country " + country["countyName"], 2);

            data = result;

            sendNotification(UPDATE, new CountryDataProvider(dictionary));
        }
    }
}
